package com.j2110.utils;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.imageio.ImageIO;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

@WebServlet("/randomCode")
public class RandomCodeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //生成随机数
        String randomCode = UUID.randomUUID().toString().substring(0, 5);

        //把随机数放进Session中
        req.getSession().setAttribute("RANDOMCODE_IN_SESSION", randomCode);

        //创建图片对象
        int width = 100;
        int height = 30;
        int imageType = BufferedImage.TYPE_INT_RGB;
        BufferedImage image = new BufferedImage(width, height, imageType);

        //画板
        Graphics g = image.getGraphics();
        g.setColor(Color.WHITE);
        //绘制一个实心的矩形
        g.fillRect(1, 1, width - 2, height - 2);

        //把随机数画进图片中
        g.setColor(Color.BLUE);//设置随机数的颜色
        Font font = new Font("宋体", Font.BOLD + Font.ITALIC, 25);
        g.setFont(font);//设置随机数的字体和大小
        g.drawString(randomCode, 10, 23);
        //干扰线
        g.setColor(Color.GRAY);
        Random r = new Random();
        for (int i = 0; i < 100; i++) {
            g.fillRect(r.nextInt(width), r.nextInt(height), 2, 2);
        }

        //关闭
        g.dispose();
        //把图片对象以流的方式保存出去
        ImageIO.write(image, "jpg", resp.getOutputStream());
    }
}
